package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.content.dto.AddCourseTeacher;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseTeacherService;
import com.xuecheng.exception.XueChengPlusException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName CourseTeacherServiceImpl
 * @Description 根据课程id查询师资信息接口实现
 * @Author CG
 * @Date 2023/11/27 15:02
 **/
@Service("courseTeacherService")
public class CourseTeacherServiceImpl implements CourseTeacherService {


    @Resource
    private CourseTeacherMapper courseTeacherMapper;

    @Override
    public List<CourseTeacher> findCourseTeacherByCourseId(Long courseId) {
        List<CourseTeacher> courseId1 = courseTeacherMapper.selectList(new QueryWrapper<CourseTeacher>().eq("course_id", courseId));
        return courseId1;
    }

    @Override
    public List<CourseTeacher> insertTeacher(AddCourseTeacher addCourseTeacher) {
        if(addCourseTeacher == null){
            XueChengPlusException.cast("教师信息不能为空");
        }
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(addCourseTeacher,courseTeacher);
        //添加成功的时间
        courseTeacher.setCreateDate(LocalDateTime.now());

        int insert = courseTeacherMapper.insert(courseTeacher);
        if(insert<=0){
            XueChengPlusException.cast("教师信息添加失败");
        }
        List<CourseTeacher> courseTeacherByCourseId = this.findCourseTeacherByCourseId(addCourseTeacher.getCourseId());
        return courseTeacherByCourseId;
    }


    @Override
    public List<CourseTeacher> updateTeacher(CourseTeacher courseTeacher) {
        if(courseTeacher == null){
            XueChengPlusException.cast("教师信息不能为空");
        }

        CourseTeacher courseTeacher1 = new CourseTeacher();
        BeanUtils.copyProperties(courseTeacher1,courseTeacher);

        int i = courseTeacherMapper.updateById(courseTeacher);
        if(i<=0){
            XueChengPlusException.cast("教师信息修改失败");
        }
        List<CourseTeacher> courseTeacherByCourseId = this.findCourseTeacherByCourseId(courseTeacher.getCourseId());
        return courseTeacherByCourseId;
    }

    @Override
    public void deleteTeacher(Long courseId,Long id) {
        /*CourseTeacher courseTeacher = courseTeacherMapper.selectOne(new QueryWrapper<CourseTeacher>().eq("course_id", courseId));
        if(courseTeacher == null){
            XueChengPlusException.cast("要删除的教师信息不存在");
        }

        if(courseTeacher.getId() != id){
            XueChengPlusException.cast("要删除的教师信息不存在");
        }*/

        //删除
        courseTeacherMapper.delete(new QueryWrapper<CourseTeacher>().eq("id", id));
    }
}
